package com.hj.dapp.business.craft.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.dapp.business.craft.mapper.CraftMapper;
import com.hj.dapp.business.craft.model.Craft;
import com.hj.dapp.business.craft.service.CraftService;
import com.hj.dapp.business.unit.model.Unit;
import com.hj.dapp.business.unit.service.UnitService;
import com.hj.dapp.common.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

import static com.hj.dapp.common.constant.LightAuxiliaryConstant.RATE_FEE;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/22
 */
@Service
public class CraftServiceImpl implements CraftService {
    @Autowired
    private UnitService unitService;
    @Autowired
    private CraftMapper craftMapper;

    @Override
    public Craft get(Long id) {
        return craftMapper.selectById(id);
    }

    @Override
    public IPage<Craft> page(PageRequest<Craft> pageRequest) {
        Page<Craft> page = new Page<>();
        page.setCurrent(pageRequest.getCurrent());
        page.setSize(pageRequest.getSize());
        QueryWrapper<Craft> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(pageRequest.getQuery())) {
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getName())) {
                queryWrapper.like("name", pageRequest.getQuery().getName());
            }
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getIllustrateType())) {
                queryWrapper.eq("illustrate_type", pageRequest.getQuery().getIllustrateType());
            }
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getBillingMethod())) {
                queryWrapper.eq("billing_method", pageRequest.getQuery().getBillingMethod());
            }
        }
        return craftMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void delete(Long id) {
        craftMapper.deleteById(id);
    }

    @Override
    public void save(Craft craft, Principal principal) {
        //如果不是费率计费方式，设置单位
        if (!RATE_FEE.equals(craft.getBillingMethod())) {
            Unit unit = unitService.get(craft.getUnit());
            craft.setUnitName(unit.getName());
            craft.setCostPrice(craft.getManCost().add(craft.getMaterialCost()));
        } else {
            craft.setCostPrice(BigDecimal.ZERO);
            craft.setSalePrice(BigDecimal.ZERO);
            craft.setNum(BigDecimal.ZERO);
        }
        craft.setCreateTime(LocalDateTime.now());
        craft.setCreateBy(principal.getName());
        craftMapper.insert(craft);
    }

    @Override
    public void edit(Craft craft, Principal principal) {
        //如果不是费率计费方式，设置单位
        if (!RATE_FEE.equals(craft.getBillingMethod())) {
            Unit unit = unitService.get(craft.getUnit());
            craft.setUnitName(unit.getName());
            craft.setCostPrice(craft.getManCost().add(craft.getMaterialCost()));
        } else {
            craft.setCostPrice(BigDecimal.ZERO);
            craft.setSalePrice(BigDecimal.ZERO);
            craft.setNum(BigDecimal.ZERO);
        }
        craft.setLastUpdateBy(principal.getName());
        craft.setLastUpdateTime(LocalDateTime.now());
        craftMapper.updateById(craft);
    }

    @Override
    public List<Craft> list(Craft craft) {
        QueryWrapper<Craft> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(craft.getName())) {
            wrapper.like("name", craft.getName());
        }
        if (!ObjectUtils.isEmpty(craft.getIllustrateType())) {
            wrapper.eq("illustrate_type", craft.getIllustrateType());
        }
        return craftMapper.selectList(wrapper);
    }
}
